<?php
/**
* @file $Id: Scores.php 161 2006-09-07 06:21:17Z doritojones $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

DrawHeader(ProgramTitle());
$extra = array();
Widgets('course');
$exams_RET = DBGet(DBQuery("SELECT ID,TITLE,SHORT_NAME,MAX_SCORE FROM ASSESSMENT_EXAMS WHERE SYEAR='".UserSyear()."'"));

// $current_extra IS SET TO $extra SO IT WILL BE AFFECTED BY Widgets()
// $current_extra is the extra information for the query that will return the current_RET data .. $extra produces the actual list
$current_extra = $extra;
$extra['columns_after'] = $extra['functions'] = array();

if(!$_REQUEST['student_id'] && !UserStudentID())
{
	switch(substr($_REQUEST['score_id'],0,1))
	{
		case '':
			if(count($exams_RET))
			{
				foreach($exams_RET as $exam)
				{
					$extra['SELECT'] .= ',NULL AS SCORE_E'.$exam['ID'];
					$extra['columns_after'] += array('SCORE_E'.$exam['ID']=>$exam['SHORT_NAME']);
					$extra['functions'] += array('SCORE_E'.$exam['ID']=>'_makeScore');			
				}
	
				$current_extra['SELECT'] .= ',asco.TYPE||asco.SCORE_ID AS SCORE_ID,asco.SCORE';
				$current_extra['FROM'] .= ',ASSESSMENT_SCORES asco,ASSESSMENT_EXAMS ae ';
				$current_extra['WHERE'] .= " AND asco.STUDENT_ID=ssm.STUDENT_ID AND asco.SCORE_ID=ae.ID AND asco.TYPE='E' AND ae.SYEAR=ssm.SYEAR";
				$current_extra['group'] = array('STUDENT_ID','SCORE_ID');
			}
		break;
		
		case 'E':
			$exam_short_name = DBGet(DBQuery("SELECT SHORT_NAME FROM ASSESSMENT_EXAMS WHERE ID='".substr($_REQUEST['score_id'],1)."'"));
			$exam_short_name = $exam_short_name[1]['SHORT_NAME'];
			
			$extra['SELECT'] .= ',NULL AS SCORE_'.$_REQUEST['score_id'];
			$extra['columns_after'] += array('SCORE_'.$_REQUEST['score_id']=>'<i>'.$exam_short_name.'</i> - Total');
			$extra['functions'] += array('SCORE_'.$_REQUEST['score_id']=>'_makeScore');
	
			$sections_RET = DBGet(DBQuery("SELECT ID,TITLE,SHORT_NAME,MAX_SCORE FROM ASSESSMENT_SECTIONS WHERE EXAM_ID='".substr($_REQUEST['score_id'],1)."'"));
			if(count($sections_RET))
			{
				foreach($sections_RET as $section)
				{
					$extra['SELECT'] .= ',NULL AS SCORE_S'.$section['ID'];
					$extra['columns_after'] += array('SCORE_S'.$section['ID']=>$section['SHORT_NAME']);
					$extra['functions'] += array('SCORE_S'.$section['ID']=>'_makeScore');			
				}
			}
	
			$current_extra['SELECT'] .= ',asco.TYPE||asco.SCORE_ID AS SCORE_ID,asco.SCORE';
			$current_extra['FROM'] .= ',ASSESSMENT_SCORES asco,ASSESSMENT_SECTIONS ae ';
			$current_extra['WHERE'] .= " AND asco.STUDENT_ID=ssm.STUDENT_ID AND ((asco.SCORE_ID=ae.ID AND asco.TYPE='S' AND ae.EXAM_ID='".substr($_REQUEST['score_id'],1)."') OR asco.SCORE_ID='".substr($_REQUEST['score_id'],1)."')";
			$current_extra['group'] = array('STUDENT_ID','SCORE_ID');
			
		break;
	}
}
else
{
	$extra['new'] = true;
	Search('student_id');
	$_REQUEST['stuid'] = UserStudentID();

	DrawHeader('Click "Back to Student List" to return to your previous search.');
	$_REQUEST['search_modfunc'] = 'list';
	$sections_RET = DBGet(DBQuery("SELECT sec.EXAM_ID,sec.ID,sec.TITLE,sec.MAX_SCORE FROM ASSESSMENT_SECTIONS sec,ASSESSMENT_EXAMS ae WHERE sec.EXAM_ID=ae.ID AND ae.SYEAR='".UserSyear()."'"),array(),array('EXAM_ID'));
	foreach($exams_RET as $exam)
	{
		$columns['SCORE_E'.$exam['ID']] = '<i>'.$exam['SHORT_NAME'].'</i>';
		$extra['SELECT'] .= ',NULL AS SCORE_E'.$exam['ID'];
		$extra['functions']['SCORE_E'.$exam['ID']] = '_makeScore';
		foreach($sections_RET[$exam['ID']] as $section)
		{
			$columns['SCORE_S'.$section['ID']] = $section['TITLE'];
			$extra['SELECT'] .= ',NULL AS SCORE_S'.$section['ID'];
			$extra['functions']['SCORE_S'.$section['ID']] = '_makeScore';
		}
	}

	$current_extra['SELECT'] .= ',asco.TYPE||asco.SCORE_ID AS SCORE_ID,asco.SCORE';
	$current_extra['FROM'] .= ',ASSESSMENT_SCORES asco ';
	$current_extra['WHERE'] .= " AND asco.STUDENT_ID=ssm.STUDENT_ID AND asco.SYEAR=ssm.SYEAR";
	$current_extra['group'] = array('SCORE_ID');
}

$current_RET = GetStuList($current_extra);

if($_REQUEST['values'] && $_POST['values'])
{
	foreach($_REQUEST['values'] as $student_id=>$scores)
	{
		foreach($scores as $score_id=>$score)
		{
			$sql = false;
			$type = substr($score_id,0,1);
			$score_type_id = $score_id;
			$score_id = substr($score_id,1);
			
			if($current_RET[$student_id][$score_type_id])
				$sql = "UPDATE ASSESSMENT_SCORES SET SCORE='".ereg_replace('[^0-9]','',$score)."' WHERE STUDENT_ID='$student_id' AND SCORE_ID='$score_id' AND TYPE='$type'";
			elseif($score!=null)
				$sql = "INSERT INTO ASSESSMENT_SCORES (STUDENT_ID,SCORE_ID,TYPE,SCORE) values('$student_id','$score_id','$type','".$score."')";
			if($sql)
				DBQuery($sql);
		}
	}
	unset($_REQUEST['values']);
	unset($_SESSION['_REQUEST_vars']['values']);
	$current_RET = GetStuList($current_extra);
}

if($_REQUEST['search_modfunc']=='list')
{
	$select = '<SELECT name=score_id onchange="this.form.submit();"><OPTION value="">All Exams</OPTION>';
	
	if(count($exams_RET))
	{
		foreach($exams_RET as $exam)
			$select .= '<OPTION value=E'.$exam['ID'].(($_REQUEST['score_id']=='E'.$exam['ID'])?' SELECTED':'').'>'.$exam['TITLE'].' ('.$exam['SHORT_NAME'].')</OPTION>';
	}
	$select .= '</SELECT>';
	
	echo "<FORM action=".PreparePHP_SELF().' method=POST>';
	DrawHeader($select,SubmitButton('Update'));
}

Search('student_id',$extra);

if($_REQUEST['search_modfunc']=='list')
{
	echo '<CENTER>'.SubmitButton('Update').'</CENTER>';
	echo '</FORM>';
}

function _makeScore($value,$column)
{	global $THIS_RET,$current_RET;

	return TextInput($current_RET[$THIS_RET['STUDENT_ID']][substr($column,6)][1]['SCORE'],'values['.$THIS_RET['STUDENT_ID'].']['.substr($column,6).']','',' size=4 maxlength=7 tabindex='.$tabindex);
}

?>